package com.hurl.waguji.biz.download;

import java.util.List;

import org.testng.log4testng.Logger;

import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjGbjgbThsBO;
import com.hurl.waguji.persistence.JcsjGbjgbThsBOId;

class ProfileGbjgThsProcessor extends BaseDownloadForUpdateProcessor {

	public ProfileGbjgThsProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}
	private static final Logger log = Logger.getLogger(ProfileGbjgThsProcessor.class);
	

	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isBlank(html)){
			return;
		}
		
		String gpdm = context.getEnvAsString("gpdm");
		
		List<String> trlist = StringUtils.substringsBetween(html, "<tr>", "</tr>");
		if(trlist==null || trlist.isEmpty()){
			return;
		}
		if(trlist.size()!=4){
			log.error("数据格式不对：行数不为4！");
			return;
		}
		
		List<String> rqlist = StringUtils.substringsBetween(trlist.get(0), "center\">","</td>");
		List<String> zgblist = StringUtils.substringsBetween(trlist.get(1), "<td><span>","</span></td>");
		List<String> ltglist = StringUtils.substringsBetween(trlist.get(2), "<td><span>","</span></td>");
		List<String> sxltglist = StringUtils.substringsBetween(trlist.get(3), "<td><span>","</span></td>");
		
		if(rqlist.size()!=zgblist.size() || rqlist.size()!=ltglist.size() || rqlist.size()!=sxltglist.size()){
			log.error("列数不对称！");
		}
		
		JcsjGbjgbThsBO bo = null;
		for(int i=0;i<rqlist.size();i++){
			bo = new JcsjGbjgbThsBO();
			bo.setId(new JcsjGbjgbThsBOId(gpdm,Formatter.toDate(rqlist.get(i))));
			bo.setGfzs(Double.valueOf(Formatter.toDouble(zgblist.get(i))*10000).longValue());
			bo.setYssltg(Double.valueOf(Formatter.toDouble(ltglist.get(i))*10000).longValue());
			bo.setSxltg(Double.valueOf(Formatter.toDouble(sxltglist.get(i))*10000).longValue());
			bo.setLastupdate(currentTime);
			
			collector.addBO(bo);
		}
	}

	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://basic.10jqka.com.cn/"+bo.getGpDmJ()+"/gbyj_iframe.html";
			}
		};
	}

}
